package com.glebusheg.utils
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Graphics;
	import flash.filters.ColorMatrixFilter;

	/**
	 * @author eliasku
	 */
	public class VisualUtil
	{
		public static function clear(container:DisplayObjectContainer, clearGraphics:Boolean = true, clearFilters:Boolean = false):void
		{
			if(container)
			{
				while(container.numChildren > 0)
					container.removeChildAt(0);

				if(clearGraphics && container.hasOwnProperty("graphics"))
				{
					const g:Graphics = container["graphics"] as Graphics;
					if(g) g.clear();
				}

				if(clearFilters && container.filters.length > 0)
					container.filters.length = 0;
			}
		}

		private static const _bwr:Number = 1 / 3;
		private static const _bwg:Number = 1 / 3;
		private static const _bwb:Number = 1 / 3;
		private static const _bwf:ColorMatrixFilter = new ColorMatrixFilter([_bwr, _bwg, _bwb, 0, 0, _bwr, _bwg, _bwb, 0, 0, _bwr, _bwg, _bwb, 0, 0, 0, 0, 0, 1, 0]);

		public static function bw(target:DisplayObject, enabled:Boolean = true):void
		{
			if(target)
			{
				if(enabled) target.filters = [_bwf];
				else target.filters = [];
			}
		}
		
		private static const _negf:ColorMatrixFilter = new ColorMatrixFilter([ -1, 0, 0, 0, 255, 0, -1, 0, 0, 255, 0, 0, -1, 0, 255, 0, 0, 0, 1, 0]);
		public static function negative(target:DisplayObject, enabled:Boolean = true):void
		{
			if(target)
			{
				if(enabled) target.filters = [_negf];
				else target.filters = [];
			}
		}
		
		private static const _desf:ColorMatrixFilter = new ColorMatrixFilter([0.309, 0.609, 0.082, 0, 0, 0.309, 0.609, 0.082, 0, 0, 0.309, 0.609, 0.082, 0, 0, 0, 0, 0, 1, 0]);
		public static function desaturate(target:DisplayObject, enabled:Boolean = true):void
		{
			if(target)
			{
				if(enabled) target.filters = [_desf];
				else target.filters = [];
			}
		}
		
		private static const _sepf:ColorMatrixFilter = new ColorMatrixFilter([0.3930000066757202, 0.7689999938011169, 0.1889999955892563, 0, 0, 0.3490000069141388, 0.6859999895095825, 0.1679999977350235, 0, 0, 0.2720000147819519, 0.5339999794960022, 0.1309999972581863, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1]);
		public static function sepia(target:DisplayObject, enabled:Boolean = true):void
		{
			if(target)
			{
				if(enabled) target.filters = [_sepf];
				else target.filters = [];
			}
		}

		public static function duplicateDisplayObject(target:DisplayObject, autoAdd:Boolean = false):DisplayObject
		{
			var duplicate:DisplayObject = null;
			if (target)
			{
				var targetClass:Class = Object(target).constructor as Class;
				duplicate = new targetClass() as DisplayObject;
					
				duplicate.transform = target.transform;
				duplicate.filters = target.filters;
				duplicate.cacheAsBitmap = target.cacheAsBitmap;
				duplicate.opaqueBackground = target.opaqueBackground;
				duplicate.scale9Grid = target.scale9Grid;

				//duplicate.graphics.copyFrom(target.graphics);

				if(autoAdd && target.parent)
					target.parent.addChild(duplicate);
			}
			return duplicate;
		}
	}
}
